# -*- coding: utf-8 -*- 

import datetime
import logging
import urllib2

from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup

from model import *

group_index = 0
group_fetch_count = 1

def update_site_sold_total():
    global group_index
    global group_fetch_count
    
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    if group_index < today_groups.count():
        group_index += group_fetch_count
    else:
        group_index = 0

    logging.info("groups count : %s %s"%(group_index,group_index+group_fetch_count))
    
    for i in range(group_index, group_index+group_fetch_count):
        group_info = today_groups[i]
        
        update_group_sold_total(group_info)
        

def update_group_sold_total(group_info):
    page = urllib2.urlopen(group_info.link)
    soup = BeautifulSoup(page)
    
    main_div = soup.findAll('div', attrs={'class':'dealinfo'})[0]
    
    sold_total = main_div.findAll('div', attrs={'class':'deal_samebox status'})[0].findAll('h6')[0].contents[0][3:][:-3]
    sold_total = int(sold_total)
    
    group_info1 = GroupInfo.get_by_id(group_info.key().id())        
    group_info1.sold_total = sold_total        
    
    group_info1.put()
    logging.info("groupinfo updated succeed : %s"%group_info.link)

def get_site():
    # create group site
    group_site = GroupSite.get_by_key_name("SITE_lashou")
    if group_site is None:
        group_site = GroupSite(key_name="SITE_lashou")
        group_site.code = "lashou"
        group_site.name = "拉手网".decode('utf8')
        group_site.link = db.Link("http://www.lashou.com/beijing")
        group_site.pop = 40
        group_site.put()
        
    return group_site

def get_data():
    # create group site
    group_site = get_site()

    # init group_info
    group_infos = list()

    # get groups
    today_groups = group_site.today_infos
    
    if today_groups.count() == 0:
        
        # get site content
        page = urllib2.urlopen(group_site.link)
        
        # soup
        soup = BeautifulSoup(page)
        
        goods = soup.findAll('div', attrs={'class':'goods'})
        for good in goods:
            good_a = good.findAll('h1')[0].findAll('a')[0]
            link = good_a['href']
            title = good_a['title']
            title = title.replace("\"", "")
            
            sold_total = good.findAll('div', attrs={'class':'deal_samebox status'})[0].findAll('h6')[0].contents[0][3:][:-3]
            
            picture = good.findAll('div', attrs={'class':'image'})[0].findAll('img')[0]['src']
        
            current_price = good.findAll('div', attrs={'class':'l price'})[0].getText()[1:]
        
            shuzi_div = good.findAll('div', attrs={'class':'shuzi'})[0]
            original_price = shuzi_div.findAll('h4')[0].getText()[1:]
            discount = shuzi_div.findAll('h3')[0].getText()[:-1]
        
            # create group_info
            group_info = GroupInfo()
            group_info.site = group_site
            group_info.title = title
            group_info.current_price = float(current_price)
            group_info.original_price = float(original_price)
            group_info.discount = float(discount)
            group_info.picture = db.Link(picture)
            group_info.link = db.Link(link)
            group_info.sold_total = int(sold_total)
            group_info.create()
            
            group_infos.append(group_info)
    else:
        for group in today_groups:
            group_infos.append(group)
        
    return group_infos


